home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / turbovis / tvtool17.zip / TVTOOLS.ZIP / STINPLIN.CPP < prev    next >
C/C++ Source or Header  |  1993-07-26  |  5KB  |  173 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #define Uses_TEvent
  4. #define Uses_TListViewer
  5. #define Uses_TPalette
  6. #define Uses_TInputRegExp
  7. #define Uses_TStaticInputLine
  8. #include "tvtools.h"
  9. #include "strings.h"
  10. #include "tools.h"
  11. #include <ctype.h>
  12.  
  13.  
  14. #define cpStaticInputLine "\x1A\x1A\x1C\x15"
  15.  
  16.  
  17. const char * const TStaticInputLine::name = "TStaticInputLine";
  18.  
  19.  
  20.  
  21. static Boolean matchChars( void *item, void *target )
  22. {
  23.  int len = strlen( (char *)target );
  24.  if ( ! len ) return (*(char *)item == EOS) ? True : False;
  25.  
  26.  return strncomp((char *)item, (char *)target, len) == 0 ;
  27. }
  28.  
  29.  
  30. TStaticInputLine::TStaticInputLine( const TRect& bounds,
  31.                                     int aMaxLen,
  32.                                     TGenCollection *aList
  33.                                   ):
  34.           TInputRegExp( bounds, aMaxLen )
  35. {
  36.  list = aList;
  37.  *testChar = EOS;
  38. }
  39.  
  40.  
  41. TStaticInputLine::TStaticInputLine( int x, int y,
  42.                                     int aMaxLen,
  43.                                     TGenCollection *aList
  44.                                   ):
  45.           TInputRegExp( x, y, aMaxLen )
  46. {
  47.  list = aList;
  48.  *testChar = EOS;
  49. }
  50.  
  51. void TStaticInputLine::setData( void *rec )
  52. {
  53.  if ( ! list ) return;
  54.  
  55.  if ( ! list->firstThat(matchChars, (char*)rec) )
  56.     (char*)rec = list->getData(0);
  57.  
  58.  TInputRegExp::setData( rec );
  59. }
  60.  
  61.  
  62. void TStaticInputLine::handleEvent(TEvent& event)
  63. {
  64.  if ( event.what == evKeyDown )
  65.     {
  66.      if ( ! list ) return;
  67.  
  68.      if ( isprint(event.keyDown.charScan.charCode) )
  69.         {
  70.          // Add character to buffer string
  71.          char *tempData = strend( testChar );
  72.          *tempData++ = event.keyDown.charScan.charCode;
  73.          *tempData = EOS;
  74.  
  75.          tempData = (char *)list->firstThat(matchChars, testChar);
  76.          if ( tempData )
  77.             {
  78.              strncpy( data, tempData, maxLen );
  79.              data[maxLen] = EOS;
  80.              selStart = 0;
  81.              selEnd = strlen(testChar);
  82.              curPos = strlen(testChar);
  83.              drawView();
  84.             }
  85.          else
  86.             {
  87.              testChar[strlen(testChar) - 1] = EOS;
  88.              buzzer();
  89.             }
  90.          clearEvent( event );
  91.          return;
  92.         }
  93.  
  94.      int off = 0;
  95.  
  96.      switch( event.keyDown.keyCode )
  97.      {
  98.       case kbLeft : off = -1;
  99.       case kbRight: if ( ! off ) off = 1;
  100.       case kbHome : if ( ! off ) off = - list->getCount();
  101.       case kbEnd  : if ( ! off ) off = list->getCount();
  102.  
  103.                     if ( list->getCount() )
  104.                        {
  105.                         ccIndex index = list->indexOf( (char *)list->firstThat(matchChars, data) );
  106.                         index += off;
  107.                     if ( index < 0 ) index = 0;
  108.                      if ( index >= list->getCount() ) index = list->getCount() - 1;
  109.                     strncpy( data, list->getData(index), maxLen );
  110.                         data[maxLen] = EOS;
  111.                        }
  112.  
  113.                     *testChar = EOS;
  114.  
  115.                 selectAll( False );
  116.                 drawView();
  117.                 clearEvent(event);
  118.                     return;
  119.  
  120.       case kbUp   :
  121.       case kbIns  : clearEvent(event);
  122.                     return;
  123.  
  124.       case kbDel: *testChar = EOS;
  125.                   *data = EOS;
  126.                    selStart = selEnd = curPos = 0;
  127.                    drawView();
  128.                    clearEvent(event);
  129.                    return;
  130.  
  131.       case kbBack: if ( *testChar ) testChar[strlen(testChar) - 1] = EOS;
  132.                    char *tempData = (char *)list->firstThat(matchChars, testChar);
  133.                    if ( ! tempData )
  134.                       tempData = (char *)list->firstThat(matchChars, "");
  135.  
  136.                    strncpy( data, tempData, maxLen );
  137.                    data[maxLen] = EOS;
  138.                    selStart = 0;
  139.                    selEnd = curPos = strlen(testChar);
  140.                    drawView();
  141.                    clearEvent(event);
  142.                    return;
  143.      }
  144.     } // if ( event.what == evKeyDown )
  145.  
  146.  if ( event.what == evMouseDown ) *testChar = EOS;
  147.  
  148.  TInputRegExp::handleEvent( event );
  149. }
  150.  
  151.  
  152. Boolean TStaticInputLine::valid( ushort command )
  153. {
  154.  if ( command == cmReleasedFocus ) *testChar = EOS;
  155.  
  156.  return TInputRegExp::valid( command );
  157. }
  158.  
  159. void TStaticInputLine::newList( TGenCollection *aList )
  160. {
  161.  if ( list ) destroy( list );
  162.  list = aList;
  163.  drawView();
  164. }
  165.  
  166. /*
  167. TPalette& TStaticInputLine::getPalette() const
  168. {
  169.   static TPalette palette( cpStaticInputLine, sizeof( cpStaticInputLine)-1 );
  170.   return palette;
  171. }
  172. */
  173.